﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twi.COMMON.Core;
using Twi.Pages.SCS.Model;
using Twi.Pages.SCS.BLL;


namespace Twi.Pages.SCS
{
    public class SingleRecord
    {
        /// <summary>
        /// 上下班打卡
        /// </summary>
        /// <returns></returns>
        public TwiReturn CheckInOut()
        {
            TwiReturn twi = new TwiReturn();
            twi.rightName = "pass";
            try
            {
                UserContext.CheckRight(ref twi);
                TUser user = (TUser)UserContext.CurrentUser;
                twi.data = _SingleRecord.Instance.CheckInOut(""+user.FID,user.FRealName);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        /// <summary>
        /// 获取当前用户打卡信息（包含：是否打卡，签到按钮显示的文字，提示信息）
        /// </summary>
        /// <returns></returns>
        public TwiReturn GetCheckInfo()
        {
            TwiReturn twi = new TwiReturn();
            twi.rightName = "pass";
            try
            {
                UserContext.CheckRight(ref twi);
                string userID = ((TUser)UserContext.CurrentUser).FID + "";
                twi.totalCount = _SingleRecord.Instance.GetStageCheckedCount(userID);
                TKindlyTip tip = _KindlyTip.Instance.GetNowKindlyTip();
                twi.data = twi.totalCount > 0 ? tip.FAfterCheckTip : tip.FBeforeCheckTip;
                twi.id = twi.totalCount > 0 ? tip.FAfterCheckBtnText : tip.FBeforeCheckBtnText;
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        /// <summary>
        /// 获取今天所有的打卡记录
        /// </summary>
        /// <returns></returns>
        public TwiReturn GetSelfTodayCheckedRecord()
        {
            TwiReturn twi = new TwiReturn();
            twi.rightName = "pass";
            try
            {
                UserContext.CheckRight(ref twi);
                string userID = ((TUser)UserContext.CurrentUser).FID + "";
                var list = _SingleRecord.Instance.GetSelfTodayCheckedRecord(userID);
                twi.totalCount = list.Count;
                twi.data = JSSerialize.Serialize(list);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        /// <summary>
        /// 获取公司今日打卡记录
        /// </summary>
        /// <returns></returns>
        public TwiReturn GetTodayAllCheckedRecord()
        {
            TwiReturn twi = new TwiReturn();
            twi.rightName = "pass";
            try
            {
                UserContext.CheckRight(ref twi);
                var list = _SingleRecord.Instance.GetTodayAllCheckedRecord();
                twi.totalCount = list.Count;
                twi.data = JSSerialize.Serialize(list);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        /// <summary>
        /// 查询指定用户指定日期段的打卡记录
        /// </summary>
        /// <returns></returns>
        public TwiReturn GetMyRecordsByCondition(string startDate, string endDate)
        {
            TwiReturn twi = new TwiReturn();
            twi.rightName = "pass";
            try
            {
                UserContext.CheckRight(ref twi);
                string userID = ((TUser)UserContext.CurrentUser).FID + "";
                var list = _SingleRecord.Instance.GetMyRecordsByCondition(userID, startDate, endDate); ;
                twi.totalCount = list.Count;
                twi.data = JSSerialize.Serialize(list);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        /// <summary>
        /// 修改打卡记录备注
        /// </summary>
        /// <param name="id"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public TwiReturn EditRemark(string id, string remark)
        {
            TwiReturn twi = new TwiReturn();
            twi.rightName = "pass";
            try
            {
                UserContext.CheckRight(ref twi);
                _SingleRecord.Instance.EditRemark(id, remark);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }
    }
}
